package com.fsi.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class GetPhotoServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		resp.setHeader("Pragma", "no-cache");
		resp.setHeader("Cache-Control", "no-cache");
		resp.setDateHeader("Expires", 0);

		String nick = req.getParameter("nick");
		// log.info("nick = " + nick);
		System.out.println("nick = " + nick);
		Photo pict = PhotoDAO.get().getUserPhoto(nick);
		if (pict != null) {
			System.out.println("pict is found, putting it to the writer. ("
					+ pict.getContentType() + ", " + pict.getFileName()
					+ ", size=" + pict.getContent().getBytes().length);
			resp.getOutputStream().write(pict.getContent().getBytes());
			resp.getOutputStream().flush();
		} else {
			// log.info("no file found");
			System.out.println("no file found");
			resp.sendRedirect("/images/noimage.jpg");
		}
	}
}
